package com.wuzhazha.javamind.chapter15.test04;

import java.util.Iterator;

/**
 * 斐波那契数列生成器，具有循环迭代功能(组合方式)
 */
public class IteratorFibonacci2  implements Iterable<Integer> {

    private Fibonacci fibonacci;

    private int n = 0 ;

    public IteratorFibonacci2(int count){
        this.n = count;
        this.fibonacci = new Fibonacci();
    }

    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            public boolean hasNext() {
                return n > 0;
            }

            public Integer next() {
                n--;
                return fibonacci.next();
            }

            public void remove() {
                throw new  UnsupportedOperationException();
            }
        };
    }
}
